home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Graphic Gems I, II & III (C_C++) / Graphics Gems C Code.sea / GemsIII / fastBitmap.c < prev    next >
Text File  |  1992-06-16  |  4KB  |  175 lines

  1. /*
  2. Fast Bitmap Stretching
  3. Tomas MÜller
  4. */
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. void Stretch(long,long,long,long,long,long);
  20. void Stretch2Lines(long,long,long,long,long,long,long,long);
  21. short ReadPixel(long x,long y);/* returns color of (x,y) in source bitmap*/
  22. void SetColor(short Col); /* set the current writing color to Col */
  23. void WritePixel(long x,long y); /* write a pixel at (x,y) in destination bitmap with the current writing color */
  24. #define sign(x) ((x)>0 ? 1:-1)
  25.  
  26. /**********************************************************
  27.  RectStretch enlarges or diminishes a source rectangle of
  28.  a bitmap to a destination rectangle. The source
  29.  rectangle is selected by the two points (xs1,ys1) and
  30.  (xs2,ys2), and the destination rectangle by (xd1,yd1) and
  31.  (xd2,yd2). Since readability of source-code is wanted,
  32.  some optimizations have been left out for the reader:
  33.  It½s possible to read one line at a time, by first
  34.  stretching in x-direction and then stretching that bitmap
  35.  in y-direction.
  36.  Entry:
  37.     xs1,ys1 - first point of source rectangle
  38.     xs2,ys2 - second point of source rectangle
  39.     xd1,yd1 - first point of destination rectangle
  40.     xd2,yd2 - second point of destination rectangle
  41. **********************************************************/
  42. void RectStretch(long xs1,long ys1,long xs2,long ys2,long xd1,long yd1,long xd2,long yd2)
  43. {
  44.     long dx,dy,e,d,dx2;
  45.     short sx,sy;
  46.     dx=abs(yd2-yd1);
  47.     dy=abs(ys2-ys1);
  48.     sx=sign(yd2-yd1);
  49.     sy=sign(ys2-ys1);
  50.     e=(dy<<1)-dx;
  51.     dx2=dx<<1;
  52.     dy<<=1;
  53.     for(d=0;d<=dx;d++)
  54.     {
  55.         Stretch(xd1,xd2,xs1,xs2,ys1,yd1);
  56.         while(e>=0)
  57.         {
  58.             ys1+=sy;
  59.             e-=dx2;
  60.         }
  61.         yd1+=sx;
  62.         e+=dy;
  63.     }
  64. }
  65.  
  66. /**********************************************************
  67.  Stretches a horizontal source line onto a horizontal
  68.  destination line. Used by RectStretch.
  69.  Entry:
  70.     x1,x2 - x-coordinates of the destination line
  71.     y1,y2 - x-coordinates of the source line
  72.     yr    - y-coordinate of source line
  73.     yw    - y-coordinate of destination line
  74. **********************************************************/
  75. void Stretch(long x1,long x2,long y1,long y2,long yr,long yw)
  76. {
  77.     long dx,dy,e,d,dx2;
  78.     short sx,sy,color;
  79.     dx=abs(x2-x1);
  80.     dy=abs(y2-y1);
  81.     sx=sign(x2-x1);
  82.     sy=sign(y2-y1);
  83.     e=(dy<<1)-dx;
  84.     dx2=dx<<1;
  85.     dy<<=1;
  86.     for(d=0;d<=dx;d++)
  87.     {
  88.         color=ReadPixel(y1,yr); 
  89.         SetColor(color);
  90.         WritePixel(x1,yw);
  91.         while(e>=0)
  92.         {
  93.             y1+=sy;
  94.             e-=dx2;
  95.         }
  96.         x1+=sx;
  97.         e+=dy;
  98.     }
  99. }
  100.  
  101. /**********************************************************
  102.  CircleStretch stretches a source rectangle, selected by
  103.  the two points (SBMINX,0) and (SBMAXX,2*r-1), onto a
  104.  Bresenham circle at (xc,yc) with radius=r.
  105.  Instead of writing pixels on the circle, horizontal lines
  106.  of the source rectangle are being stretched onto all
  107.  horizontal lines of the circle. 
  108.  Entry:
  109.     SBMINX - min x of source rectangle
  110.     SBMAXX - max x of source rectangle 
  111.     xc,yc  - center of the circle
  112.     r      - radius of circle
  113. **********************************************************/
  114. void CircleStretch(long SBMINX,long SBMAXX,long xc,long yc,long r)
  115. {
  116.     long p=3-(r<<1),x=0,y=r;
  117.     while(x<y)
  118.     {
  119.         /* stretch lines in first octant */
  120.         Stretch2Lines(xc-y,xc+y,SBMINX,SBMAXX,r-x,yc-x,r+x,yc+x);
  121.         if(p<0) p=p+(x<<2)+6;
  122.         else
  123.         {
  124.             /* stretch lines in second octant */
  125.             Stretch2Lines(xc-x,xc+x,SBMINX,SBMAXX,r-y,yc-y,r+y,yc+y);
  126.             p=p+((x-y)<<2)+10;
  127.             y--;
  128.         }
  129.         x++;
  130.     }
  131.     if(x==y) Stretch2Lines(xc-x,xc+x,SBMINX,SBMAXX,r-y,yc-y,r+y,yc+y);
  132. }
  133.  
  134. /**********************************************************
  135.  Stretch2Lines stretches two source lines with same length
  136.  and different y-coordinates onto two destination lines
  137.  with same length and different y-coordinates. Used by
  138.  CircleStretch.
  139.  Entry:
  140.     x1,x2 - x-coordinates of the destination line
  141.     y1,y2 - x-coordinates of the source line
  142.     yr1   - y-coordinate of source line # 1
  143.     yw1   - y-coordinate of destination line # 1
  144.     yr2   - y-coordinate of source line # 2
  145.     yw2   - y-coordinate of destination line # 2
  146. **********************************************************/
  147. void Stretch2Lines(long x1,long x2,long y1,long y2,long yr1,long yw1,long yr2,long yw2)
  148. {
  149.     long dx,dy,e,d,dx2;
  150.     short sx,sy,color;
  151.     dx=abs(x2-x1);
  152.     dy=abs(y2-y1);
  153.     sx=sign(x2-x1);
  154.     sy=sign(y2-y1);
  155.     e=(dy<<1)-dx;
  156.     dx2=dx<<1;
  157.     dy<<=1;
  158.     for(d=0;d<=dx;d++)
  159.     {
  160.         color=ReadPixel(y1,yr1);
  161.         SetColor(color);
  162.         WritePixel(x1,yw1);
  163.         color=ReadPixel(y1,yr2);
  164.         SetColor(color);
  165.         WritePixel(x1,yw2);
  166.         while(e>=0)
  167.         {
  168.             y1+=sy;
  169.             e-=dx2;
  170.         }
  171.         x1+=sx;
  172.         e+=dy;
  173.     }
  174. }
  175.